<!--
 * @Description: 模型选择
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-05-08 10:51:06
 * @FilePath: /yuxiuproweb/src/views/process/vm/mods/part.vue
-->

<template>
  <el-dialog
    :close-on-click-modal="false"
    title="模型选择"
    :visible.sync="shows"
    width="800px"
    append-to-body
  >
    <div class="part">
      <div class="part-left">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="模型名称" prop="modelName">
            <el-input
              v-model="queryParams.modelName"
              placeholder="请输入模型名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-table ref="multipleTable" :data="fabricList" :max-height="312">
          <el-table-column width="60" align="center">
            <template slot-scope="{ row }">
              <el-radio
                v-model="row.checked"
                class="radio-view"
                :label="true"
                @change="checkedChange(row)"
                ><i></i
              ></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="模型名称" prop="modelName"></el-table-column>
          <el-table-column label="类型" prop="type" width="160">
            <template slot-scope="{ row }">
              {{ getTypeName(TYPES, row.type) }}
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listModel } from "@/api/process/vm";
import { TYPES } from "../configure";
export default {
  props: {
    show: Boolean,
    type: {
      type: String,
      default: "add",
    },
    data: {
      type: Object,
    },
  },
  data() {
    return {
      TYPES,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        modelName: null,
      },
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 面料维护表格数据
      fabricList: [],
      checkedRow: null,
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询面料维护列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(({ rows, total }) => {
        this.fabricList = rows?.map((e) => ({
          ...e,
          checked:
            this.checkedRow?.mdWindowCurtainModelId ===
            e.mdWindowCurtainModelId,
        }));
        this.total = total;
        this.loading = false;
      });
    },
    checkedChange(row) {
      this.fabricList?.forEach((e) => {
        if (e.mdWindowCurtainModelId !== row.mdWindowCurtainModelId)
          e.checked = false;
      });
      this.checkedRow = {
        ...row,
      };
    },
    /** 提交按钮 */
    submitForm() {
      if (this.checkedRow) {
        this.$emit("on-change", this.checkedRow);
        this.shows = false;
      } else {
        this.$message.error("请选择主布");
      }
    },
    handleClose(tag, index) {
      this.multipleSelection?.splice(index, 1);
      this.toggleSelection([tag]);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
  },
};
</script>

<style scoped lang="scss">
.part {
  width: 100%;
  display: flex;
  &-left {
    width: 100%;
  }
}
</style>
